package com.qijian.maindata.tools.xls;

import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.enums.CellExtraTypeEnum;
import com.alibaba.excel.metadata.CellExtra;
import com.alibaba.excel.read.listener.ReadListener;
import com.qijian.maindata.domain.excel.InspectionExcelVo;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Slf4j
public class InspectionExcelReadListener implements ReadListener<InspectionExcelVo> {

    //解析的最终数据
    @Getter
    private List<InspectionExcelVo> list = new ArrayList<>();

    //内容起始行
    private Integer headerRowIndex;

    //合并单元格信息
    private List<CellExtra> extraMergedList = new ArrayList<>();

    public InspectionExcelReadListener(Integer headerRowIndex) {
        this.headerRowIndex = headerRowIndex;
    }


    @Override
    public void invoke(InspectionExcelVo data, AnalysisContext context) {
        //每一行的数据
        list.add(data);
    }

    @Override
    public void extra(CellExtra extra, AnalysisContext context) {
        //读取到了合并单元格信息
        if (Objects.nonNull(extra) && extra.getType() == CellExtraTypeEnum.MERGE) {
            if (extra.getRowIndex() >= headerRowIndex) {
                extraMergedList.add(extra);
            }
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        //数据处理完了,合并数据
        explainMergeData();
    }

    private void explainMergeData() {
        extraMergedList.forEach(cellExtra -> {
            int firstRowIndex = cellExtra.getFirstRowIndex() - headerRowIndex;
            int lastRowIndex = cellExtra.getLastRowIndex() - headerRowIndex;
            int firstColumnIndex = cellExtra.getFirstColumnIndex();
            int lastColumnIndex = cellExtra.getLastColumnIndex();
            //获取初始值
            Object initValue = getInitValueFromList(firstRowIndex, firstColumnIndex, list);
            //设置值
            for (int i = firstRowIndex; i <= lastRowIndex; i++) {
                for (int j = firstColumnIndex; j <= lastColumnIndex; j++) {
                    setInitValueToList(initValue, i, j, list);
                }
            }
        });
    }

    private void setInitValueToList(Object filedValue, Integer rowIndex, Integer columnIndex,
                                    List<InspectionExcelVo> data) {
        InspectionExcelVo object = data.get(rowIndex);
        for (Field field : object.getClass().getDeclaredFields()) {
            field.setAccessible(true);
            ExcelProperty annotation = field.getAnnotation(ExcelProperty.class);
            if (annotation != null) {
                if (annotation.index() == columnIndex) {
                    try {
                        field.set(object, filedValue);
                        break;
                    } catch (IllegalAccessException e) {
                        log.error("设置合并单元格的值异常：{}", e.getMessage());
                    }
                }
            }
        }
    }

    private Object getInitValueFromList(Integer firstRowIndex, Integer firstColumnIndex,
                                        List<InspectionExcelVo> data) {
        Object filedValue = null;
        InspectionExcelVo object = data.get(firstRowIndex);
        for (Field field : object.getClass().getDeclaredFields()) {
            field.setAccessible(true);
            ExcelProperty annotation = field.getAnnotation(ExcelProperty.class);
            if (annotation != null) {
                if (annotation.index() == firstColumnIndex) {
                    try {
                        filedValue = field.get(object);
                        break;
                    } catch (IllegalAccessException e) {
                        log.error("设置合并单元格的初始值异常：{}", e.getMessage());
                    }
                }
            }
        }
        return filedValue;
    }
}
